using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Linq;

public class MainMgr : MonoBehaviour
{
    public InputField if_target;
    public InputField if_split;
    public InputField if_out;

    public Text tipTxt;
    

    public void Run()
    {
        string str1 = if_target.text.Trim();
        if (str1 == "")
        {
            Tip("目标路径输入错误");
            return;
        }
        string str2 = if_split.text.Trim();
        if (str2 == "")
        {
            Tip("分割标志输入错误");
            return;
        }
        string str3 = if_out.text.Trim();
        if (str3 == "")
        {
            Tip("输出路径输入错误");
            
            return;
        }

        fixedPath(str1);
        fixedPath(str3);
        if (Directory.Exists(str1) == false || Directory.Exists(str1) == false)
        {
            Tip("目录不存在，");
            return;
        }

        //string str1 = "C:\\Users\\justi\\Downloads\\tmp";
         //str2 = "绘画\n漫画";
        //string str3 = "C:\\Users\\justi\\Downloads\\dd";
        var flgArr= str2.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
        string[] pdfFiles = Directory.GetFiles(str1, "*.pdf", SearchOption.AllDirectories);
        
        List<string> wantpdffiles = new List<string>();
        List<string> dontHavepdffiles = new List<string>();
        HashSet<string> hash = new HashSet<string>();
        // 遍历并输出每个PDF文件的路径
        foreach (string filePath in pdfFiles)
        {
            //Debug.Log("PDF文件路径: " + filePath+":"+Path.GetFileName(filePath));
            var fileName = Path.GetFileName(filePath);
            foreach (var flg in flgArr)
            {
                if (fileName.Contains(flg))
                {
                    wantpdffiles.Add(filePath);
                    hash.Add(flg);
                }
                /*else
                {
                    Debug.Log("dont contain flg"+flg);
                    if (dontHavepdffiles.Contains(flg) == false)
                    {
                        dontHavepdffiles.Add(flg);
                    }
                }*/
            }
            // 这里你可以添加处理每个PDF文件的逻辑，例如读取、复制或移动
        }

        foreach (var flg in flgArr)
        {
            if (!hash.Contains(flg))
            {
                if(!dontHavepdffiles.Contains(flg)) 
                    dontHavepdffiles.Add(flg);
            }
        }
        
        print(wantpdffiles.Count);
        foreach (var want in wantpdffiles)
        {
            File.Copy(want,Path.Combine(str3,Path.GetFileName(want)),true);
        }

        string tt = string.Join("\n",dontHavepdffiles);
        Tip("成功!以下标识是没有找到的:\n"+tt);
    }

    public string fixedPath(string path)
    {
       // string str1 = path.Replace("\"", "");
       string str1 = path;
        string str2 = str1.Replace("\\", "\\\\");
        return str2;
    }

    private IEnumerator tipText(string content)
    {
        tipTxt.text = content;
        yield return new WaitForSeconds(4.5f);
        tipTxt.text = "";
    }

    public void Tip(string str)
    {
        StartCoroutine(tipText(str));
    }
    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
